import { assingRoleSaveApi, getRoleIdApi } from '@/api/system/user/user'
import { RoleListParam } from '@/api/system/role/RoleModel'
import { SelectRoleParam } from '@/api/system/user/UserModel'
import useInstance from '@/hooks/useInstance'
import { StatusCode } from '@/http/request'
import { DialogModel } from '@/types/BaseType'
import { nextTick, onMounted, reactive, ref } from 'vue'
import { getRoleListApi } from '@/api/system/role/role'
import {
    CustcareListParam,
    TrailNewCustcare
} from '@/api/custcare/visitor/VisitorModel'
import {
    getCustcareListApi,
    trailNewCustcareApi
} from '@/api/custcare/visitor/visitor'
import { custCareUserStore } from '@/store/custcare'
import { SendMessage } from '@/api/custcare/talk/BackUserModel'
import { getCustcareUserInfo } from '@/utils/custcareAuth'
import { MESSAGE_CMD, MESSAGE_TYPE } from '@/types/CustcareEnum'

export default function useTrailCustcare(
    dialogModel: DialogModel,
    onShow: any,
    onClose: any,
    emit: any
) {
    const { global } = useInstance()
    const custStore = custCareUserStore()

    // 表格高度
    const tableHeight = ref(0)

    const oldChannelId = ref('')

    const selectHasId = ref(0)

    const toUserUUid = ref('')

    // 列表查询参数
    const custcareParams = reactive<CustcareListParam>({
        current_page: 1,
        page_size: 8,
        total: 0
    })

    // 列表数据
    const custcareList = reactive({
        list: []
    })

    const onConfirm = async () => {
        if (!selectHasId.value) {
            global.$message({ message: '请选择客服', type: 'warning' })
            return false
        }
        let param = reactive<TrailNewCustcare>({
            old_channel_id: oldChannelId.value,
            has_id: selectHasId.value
        })
        console.log('转接：', param)
        let res = await trailNewCustcareApi(param)
        if (res && res.code == StatusCode.Success) {
            // 通知访客
            const msg = reactive<SendMessage>({
                cmd: MESSAGE_CMD.CMD_MSG,
                channel_id: res.data.channel_id,
                to_user_uuid: toUserUUid.value,
                type: MESSAGE_TYPE.TYPE_NOTIFY_REPLACE_SERVICE,
                content: '转接到新客服'
            })
            custStore.getSocket.sendMessage(msg)

            emit('on-trail', '刷新对话列表')

            global.$message({ message: res.msg, type: 'success' })
            onClose()
        } else {
            global.$message({ message: res.msg, type: 'error' })
        }
    }
    const show = async (clientIp: string, channelId: string, uuid: string) => {
        oldChannelId.value = channelId
        toUserUUid.value = uuid
        // 设置弹框标题
        dialogModel.title = '为【' + clientIp + '】转接客服'
        // 显示弹框
        onShow()
    }

    // 获取列表数据
    const getCustcareList = async () => {
        let res = await getCustcareListApi(custcareParams)
        if (res && res.code == StatusCode.Success) {
            custcareList.list = res.data.list
            custcareParams.total = res.data.total
        }
    }

    const sizeChange = (size: number) => {
        custcareParams.page_size = size
        getCustcareList()
    }

    const currentChange = (page: number) => {
        custcareParams.current_page = page
        getCustcareList()
    }

    const getSelectCustcare = (id: number) => {
        selectHasId.value = id
    }

    const cellClickHandle = (row: any) => {
        getSelectCustcare(row.id)
    }

    onMounted(() => {
        nextTick(() => {
            tableHeight.value = window.innerHeight - 490
        })
        getCustcareList()
    })

    return {
        onConfirm,
        show,
        custcareParams,
        custcareList,
        sizeChange,
        currentChange,
        tableHeight,
        selectHasId,
        getSelectCustcare,
        cellClickHandle
    }
}
